package com.fancy.learncenter.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.AudioBean;
import com.fancy.learncenter.bean.AudioDataBean;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.NGKListDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.LoginActivity;
import com.fancy.learncenter.ui.adapter.FragmentOnlineAudioAdapter;
import com.fancy.learncenter.ui.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.ui.adapter.base.CustomViewHold;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class OnlineAuidoFragment extends Fragment {


    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;

    FragmentOnlineAudioAdapter listAdapter;
    List<AudioBean> listDatas;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_online_auido, container, false);
        ButterKnife.bind(this, view);
        initRecycle();
        getListData();
//        up();
        return view;
    }

    private void initRecycle() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);

        listDatas = new ArrayList<>();
        listAdapter = new FragmentOnlineAudioAdapter(getActivity(), listDatas);


        recyclerView.setAdapter(listAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


    private void getListData() {
        Map map = new HashMap();
        HttpMehtod.getInstance().getOnlineRecorderAudio(map, new CustomNetSubscriber(getActivity()) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {
                //解析数据
                BaseBean<AudioDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), AudioDataBean.class);

                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<AudioDataBean>() {
                    @Override
                    public void success(AudioDataBean dataBean) {
                        if (dataBean.getResourceVOList() != null) {
                            listAdapter.notifyDataSetChanged(dataBean.getResourceVOList());
                        }

                    }

                    @Override
                    public void fail(String errorCode) {

                        if (errorCode.equals(Constant.ERROR_LOGIN_CODE)) {
                            Intent intent = new Intent(getActivity(), LoginActivity.class);
                            startActivityForResult(intent, Constant.FRESH_CALLBACK);
                        }

                    }

                }).customDealData();
            }

            @Override
            public void onCompleted() {
                super.onCompleted();
            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == Constant.FRESH_CALLBACK && resultCode == Constant.FRESH_RESULT_CALLBACK) {
            if (LoginUserSharePrefernce.isLogin()) {
                getListData();
            }
        }

    }
}
